package com.wondedDream.repository;

import com.wondedDream.model.UserMockExamQuestionRecord;
import com.wondedDream.model.UserPracticeRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserMockExamQuestionRecordRepository extends JpaRepository<UserMockExamQuestionRecord, Integer> {
    // 这里假设主键是 String 类型，如果是 Long 或其他类型，请相应修改
    @Query("SELECT umqr FROM UserMockExamQuestionRecord umqr WHERE umqr.mockExam.id = :userMockExamRecordId")
    List<UserMockExamQuestionRecord> findByUserMockExamRecordId(Integer userMockExamRecordId);

    @Query("SELECT umqr FROM UserMockExamQuestionRecord umqr WHERE umqr.mockExamQuestion.id = :mockExamQuestionId")
    List<UserMockExamQuestionRecord> findByMockExamQuestionId(String mockExamQuestionId);

    @Query("SELECT DISTINCT u FROM UserMockExamQuestionRecord u JOIN u.mockExam m WHERE u.user.openid = :userOpenId AND m.id IN :mockExamIds")
    List<UserMockExamQuestionRecord> findByUserOpenIdAndMockExamId(String userOpenId,List<Integer> mockExamIds);

    @Query("SELECT umqr FROM UserMockExamQuestionRecord umqr WHERE umqr.mockExam.id = :userMockExamRecordId AND umqr.user.openid = :userOpenId")
    List<UserMockExamQuestionRecord> getUserMockExamQuestionRecordByIdAndOpenid(Integer userMockExamRecordId, String userOpenId);

    @Query("SELECT umqr FROM UserMockExamQuestionRecord umqr WHERE umqr.mockExam.id = :userMockExamRecordId AND umqr.user.openid = :userOpenId AND umqr.isCorrect = false")
    List<UserMockExamQuestionRecord> getUserErrorMockExamQuestionRecordByIdAndOpenid(Integer userMockExamRecordId, String userOpenId);

    @Query("DELETE FROM UserMockExamQuestionRecord u WHERE u.user.openid = :openid AND u.mockExam.id = :mockExamId")
    void deleteByUserIdAndMockExamId(String openid, Integer mockExamId);
}